<?php
namespace Admin\Event;
use Think\Controller;
class UserEvent extends Controller
{
    public function index(){
        $user = M('user');
//        $list = $user->order('id')->page(($_GET['p']?$_GET['p']:1).',3')->select();
        $count= $user->count();// 查询满足要求的总记录数
        $Page= new \Think\Page($count,9);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show= $Page->show();// 分页显示输出
        $list = $user->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();
//        $Model->alias('a')->join('__DEPT__ b ON b.user_id= a.id')->select();
//        dump($list);die;
        $this->assign('res',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
    }
    public function adddo(){

        $user = M('user'); // 实例化User对象
        $data['name'] = $_POST['name'];
        $name = $_POST['name'];
        $data['pwd'] = md5($_POST['pwd']);
        $data['tel'] = $_POST['tel'];
        $data['sex'] = $_POST['sex'];
        $data['email'] = $_POST['email'];
        $data['create_time'] =date('Y-m-d H:i:s');
        //上传头像
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     'user/'; // 设置附件上传（子）目录
        $img_info   =   $upload->upload();
        $data['user_img'] = $img_info['photo']['savename'];
//        //缩略图片
//        $image = new \Think\Image();
//        dump('./Uploads/'.$img_info['photo']['savepath'].$data['user_img']);
//        //图片路径
//        $file_path = './Uploads/user'.$img_info['photo']['savepath'].$data['user_img'];
//        //缩略图路径
//        $file_mini = './__ROOT__/Uploads/user/'.$img_info['photo']['savepath'].'mini/'.$data['user_img'];
//        $image->open('./__ROOT__/Uploads/user/2018/01/24/5a6864690d87c.jpg');
////        $image->open($file_path);
//// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
//        $image->thumb(50, 50)->save($file_mini);
////        if(!$info) {// 上传错误提示错误信息
////            $this->error($upload->getError());
////        }else{// 上传成功
////            $this->success('上传成功！');
////        }
        //判断用户是否存在
        $res = M('user')->field(array('id'))->where('name='."'$name'")->select();
        $res = $res[0];
//        print_r($res);
        if($res){
            $this->error('用户名已存在',U('User/add'));
        }else{
            //用户名不存在则可以插入
            //插入数据库
            $res = $user->add($data);
            if ($res>0){
                $this->success('添加成功',U('User/index'));
            }else{
                $this->success('添加失败',U('User/index'));
            }

        }
    }
    public function admin_index(){
//        $res = M('adminuser')->select();
//        $this->assign('res',$res);
        $user = M('adminuser');
        $list = $user->order('create_time')->page(($_GET['p']?$_GET['p']:1).',3')->select();
        $count= $user->count();// 查询满足要求的总记录数
        $Page= new \Think\Page($count,9);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show= $Page->show();// 分页显示输出
        $list = $user->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('res',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
    }
    public function admin_adddo(){
//        dump($_POST);die;
        $user = M('adminuser'); // 实例化User对象
        $data['name'] = $_POST['name'];
        $name = $_POST['name'];
        $data['pwd'] = md5($_POST['pwd']);
        $data['tel'] = $_POST['tel'];
        $data['type'] = $_POST['type'];
        $data['email'] = $_POST['email'];
        $data['create_time'] =date('Y-m-d H:i:s');
        //判断用户是否存在
        $res = M('adminuser')->field(array('id'))->where('name='."'$name'")->select();
        $res = $res[0];
//        print_r($res);
        if($res){
            $this->error('管理员已存在',U('User/admin_add'));
        }else{
            //用户名不存在则可以插入
            //插入数据库
            $res = $user->add($data);
            if ($res>0){
                $this->success('添加管理员成功',U('User/admin_index'));
            }else{
                $this->success('添加管理员失败',U('User/admin_index'));
            }

        }
    }


}